<%@page contentType="text/html" pageEncoding="ISO-8859-1"%>
<%@page import="Problemdomeneklasser.*, Kontroller.Oversikt"%>

<!DOCTYPE html>
<html lang="no">
    <head>
        <title>askMe - answerMe.</title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <script src="resources/js/jquery.min.js"></script>
        <script src="resources/js/askMe.js"></script>
        <link rel="stylesheet" type="text/css" href="resources/css/default.css" />
    </head>
    <body>
        <div id="menu">
            <jsp:include page="deler/menu.jsp" />
        </div>
        <article id="content">
        </article>
        
        <%
            Oversikt o = new Oversikt();
            String sLenke = request.getParameter("sLenke");
            int svart = -1;
            String jsSpm = null;
            if (sLenke != null) {
                Sporsmaal spm = o.hentSporsmaalURL(sLenke);
                if (spm != null) {
                    String sID = spm.getSid() + "";
                    jsSpm = spm.toJavaScript();
                    String[] altNavn = request.getParameterValues(sID);
                    if (session.getAttribute(sID) == null) {
                        if (altNavn != null) {
                            o.registrerSvar(spm, altNavn);
                            session.setAttribute(sID, altNavn);
                            svart = 0;
                        }
                    } else {
                        //Svar ikke sendt pga har allerede svart.
                        svart = 1;
                    }
                }
            }
        %>
        
        <script>            
            var q = <%=jsSpm%>
            var table = $('<table/>');
            var form = $('<form/>').attr('action', q.lenke).attr('method', 'post')
            .append($('<h3/>').html(q.navn+') '+q.tekst));   
            
            table.append($('<tr/>'));
            //table.append($('<tr/>').append($('<td/>').html('--------------------')));
            var qa = q.altArray;
            var type = q.radio ? 'radio':'checkbox';
            
            for(var i = 0; i< qa.length; ++i){
                var tr = $('<tr/>').attr('id', qa[i].aid);
                table.append(tr.append($('<td/>').html(qa[i].navn+') '+qa[i].tekst)));
                table.append(tr.append($('<input/>').attr('type', type)
                .attr('label',qa[i].aid).attr('name', q.sid).attr('value', qa[i].navn)));
            }
            //table.append($('<tr/>').append($('<td/>').html('--------------------')))
            table.append( $('<tr/>').append($('<td/>').append($('<button/>').html('Send'))));  
            
            var content = $('#content');
            $('<div/>').addClass('question').append(form.append(table)).appendTo(content); 
            
            var svart = <%=svart%>;
            if(svart >= 0){
                var cssClass = svart == 0 ? 'message' : 'warning';
                var msg = svart == 0 ? 'Svar sendt' : 'Du har allerede svart!';
                content.append($('<div/>').addClass(cssClass).html(msg));
            }            
        </script>
    </body>
</html>

